<?php
/*
 *  Field setting
*/
class FieldController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;
	protected $class;
	
	public function init() {
		$this->_user = $this->checkLogin ();
		//print_r($this->_user);exit;		
		/* Initialize action controller here */
		$this->controller = $this->getRequest ()->getControllerName ();
		$this->action = $this->getRequest ()->getActionName ();
		
		$this->commonTemplates ();
		
		$this->_smarty->assign ( 'controller', $this->controller );
		$this->_smarty->assign ( 'action', $this->action );
		$this->_smarty->assign ( '_user', $this->_user );
		
		$this->_smarty->assign ( 'title', 'Field Management' );
		
		//get database
		$this->getDb ( 'osmdb' );
		
	//verify rights
	//if (isset ( $this->_user->agentcode ))
	//	$this->_redirect ( '/' );
	}
	
	public function indexAction() {
		try {
			$count = 20; //pre
			$curpage = ( int ) $this->_request->getParam ( 'page' );
			if (! $curpage)
				$curpage = 1;
			$offset = ($curpage - 1) * $count;
			
			$sql = 'select SQL_CALC_FOUND_ROWS * from arafmi_field where branch_id = '. $this->_user->branch_id. ' order by field_type asc, field_order asc ' . " limit $offset,$count";
			$result = $this->_dbAdapter->fetchAll ( $sql );
			
			//foreach ( $result as $key => $value ) {
			//	$result [$key] ['disabled'] = $value ['isdisabled'] ? '无效' : '有效';
			//}
			
			//翻页控制
			$param ['file'] = '/' . $this->controller . '/' . $this->action;
			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' );
			$param ['perpagenum'] = $count; // 每页显示的数目
			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
			$param ['curpage'] = $curpage; // 当前页码
			$this->_smarty->assign ( 'allnum', $param ['totalnum'] );
			
			$page = new MyClass_Page ( $param );
			//$page->setvar ( array ('gid' => $gradeid,'typeid'=>$typeid) );
			

			$this->_smarty->assign ( 'page', $page->getNumPage () );
			
			$this->_smarty->assign ( 'result', $result );
			$this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', 'javascript:window.history.back();', 'warning' );
		}
	}
	//add field
	public function addAction() {
		try {
			$sql = "select field_ID, field_name from arafmi_field where field_type = 'Field' order by field_order asc";
			$field_type = $this->_dbAdapter->fetchPairs ( $sql );
			$this->_smarty->assign ( 'field_type', $field_type );				
			$this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//operate to add field 
	public function opaddAction() {
		try {
			if (! $this->isPost ()) {
				throw new Exception ( 'Parameter or argument error' );
			}
			
			$p = $this->_request->getPost ();
			if(empty($p['field_type']))
				throw new Exception ( 'Field Type can not be empty!' );
			//get branch_id
			$sql = 'select branch_id from arafmi_users where uid = '.$this->_user->uid;
			//echo $sql;
			$branchinfo = $this->_dbAdapter->fetchRow ( $sql );
			$p['branch_id'] = $branchinfo['branch_id'];
			//print_r($p);exit;
			//if (! isset ( $p ['isdisabled'] ))
			//	$p ['isdisabled'] = 0;
			
			$result = $this->_dbAdapter->insert ( 'arafmi_field', $p );
			if ($result) {
				$this->feedback ( 'Saved successfully!', 'Success', '/' . $this->controller, 'tip', 0 );
			} else {
				throw new Exception ( 'Saved failed!' );
			}
		
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//edit field action
	public function editAction() {
		try {
			
			$id = ( int ) $this->_request->getParam ( 'field_ID' );
			if (! $id)
				throw new Exception ( 'Parameter or argument error!' );
			//get field list	
			$sql = "select field_name, field_name from arafmi_field where field_type = 'Field' order by field_order asc";
			$field_type = $this->_dbAdapter->fetchPairs ( $sql );
			$this->_smarty->assign ( 'field_type', $field_type );
			
			$sql = 'select * from arafmi_field where field_ID=' . $id;
			$result = $this->_dbAdapter->fetchRow ( $sql );
			//print_r($field_type);exit;
			$this->_smarty->assign ( 'result', $result );			
			//$this->_smarty->assign ( 'field_type_selected', $result ['field_type'] );					
			
			$this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//operate to edit field
	public function opeditAction() {
		try {
			if (! $this->isPost ()) {
				throw new Exception ( 'Parameter or argument error!' );
			}
			
			$p = $this->_request->getPost ();
			
			if(empty($p['field_type']))
				throw new Exception ( 'Field Type can not be empty!' );
				
			//print_r($p);exit;	
			$result = $this->_dbAdapter->update ( 'arafmi_field', $p, 'field_ID=' . $p ['field_ID'] );
			if ($result) {
				$this->feedback ( 'Saved successfully!', 'Success', '/' . $this->controller, 'tip', 0 );
			} else {
				throw new Exception ( 'Saved failed!' );
			}
		
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//delete field
	public function deleteAction() {
		try {
			$id = ( int ) $this->_request->getParam ( 'field_ID' );
			if (! $id)
				throw new Exception ( 'Parameter or argument error!' );
			
			$del_result = $this->_dbAdapter->delete ( 'arafmi_field', 'field_ID=' . $id );
			if ($del_result) {
				$this->feedback ( 'Deleted successfuly, please refresh to check!', 'Success', '/' . $this->controller, 'tip', 0 );
			} else {
				throw new Exception ( 'Deleted failed or have already deleted, please refresh to check!');
			}
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//reserve statement as below
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		
		throw new Exception ( 'Invalid method' );
		
		// all other methods throw an exception
		throw new Exception ( 'Invalid method "' . $method . '" called' );
	}
}
